package com.margo.project.web.validator.customizer;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * https://www.cnblogs.com/xiaogangfan/p/5987659.html
 * 自定义校验规则
 * @author fangxingbang
 *
 */
public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {

	private CaseMode caseMode;

	public void initialize(CheckCase constraintAnnotation) {
		this.caseMode = constraintAnnotation.value();
	}

	@Override
	public boolean isValid(String object, ConstraintValidatorContext context) {
		if (object == null)
			return true;

		if (caseMode == CaseMode.UPPER)
			return object.equals(object.toUpperCase());
		else
			return object.equals(object.toLowerCase());
	}

}
